package com.crm.controller;

import com.crm.domain.DataGridView;
import com.crm.pojo.Role;
import com.crm.service.RoleService;
import com.crm.service.UserService;
import com.crm.util.ResultDto;
import com.crm.vo.RoleVo;
import com.crm.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController extends BaseController{
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @GetMapping(value = "/users.do")
    public DataGridView getUserList(UserVo userVo){
        logger.debug("**********getUserList***********");
        logger.debug("电话："+userVo.getTelephone());
        logger.debug("姓名:"+userVo.getRealname());
        return userService.getUserListByPage(userVo);
    }
    @PostMapping(value = "/user/insertUser.do",produces = "application/json;charset=UTF-8")
    public ResultDto insertUser(UserVo userVo){
        ResultDto dto=new ResultDto();
        logger.debug("***********addUser*****************");
        logger.debug("电话："+userVo.getTelephone());
        logger.debug("姓名:"+userVo.getRealname());
        logger.debug("密码:"+userVo.getPassword());
        logger.debug("生日:"+userVo.getBirthday());
        logger.debug("上传图片的路径");
        logger.debug("部门编号:"+userVo.getDept().getId());
        logger.debug("状态:"+userVo.getAvailable());
        logger.debug("************************************");
        boolean flag=this.userService.addUser(userVo);
        if(flag) {
            dto.setCode(200);
            dto.setMsg("增加成功");
        }
        return dto;
    }
    @GetMapping(value = "/user/deleteuser.do")
    public ResultDto deleteUser(Integer id){
        ResultDto dto=new ResultDto();
        try {
            this.userService.removeUser(id);
            dto.setCode(200);
            dto.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode(-1);
            dto.setMsg("删除成功");
        }
        return dto;
    }
    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @RequestMapping(value = "/user/deleteBatchUser.do")
    @ResponseBody
    public ResultDto deleteBatchUser(@RequestParam("ids[]")Integer[] ids){
        System.out.println("*************"+ids);
        ResultDto dto=new ResultDto();
        try {
            this.userService.removeBatchUser(ids);
            dto.setCode(200);
            dto.setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            dto.setCode(-1);
            dto.setMsg("删除失败");
        }
        return dto;
    }
    @RequestMapping(value = "/user/initRoleByUserId")
    public DataGridView initRoleByUserId(Integer uid){
        //查询所有的角色
        List<Role> allRoleList= roleService.findAllRoles();
        //查询当前用户所拥有的角色的集合
        List<Role> currentUserRoles =roleService.selectRolesByUid(uid);
        List<RoleVo> roleBeanList=new ArrayList<RoleVo>();
        for(Role role:allRoleList){
            Boolean LAY_CHECKED=false;
            Integer roleId=role.getId();
            for(Role roleItem:currentUserRoles){
                if(roleItem.getId()==roleId){
                    LAY_CHECKED=true;
                    break;
                }
            }
            RoleVo roleBean=new RoleVo();
            roleBean.setId(role.getId());
            roleBean.setRolename(role.getRolename());
            roleBean.LAY_CHECKED=LAY_CHECKED;
            roleBeanList.add(roleBean);
        }
        return new DataGridView(Long.valueOf(roleBeanList.size()),roleBeanList);
    }
    @RequestMapping(value = "/user/assignRole.do")
    @ResponseBody
    public ResultDto assignRight(@RequestParam("rids[]")Integer[] rids,
                                 @RequestParam("uid") Integer uid){
        ResultDto resultDto=new ResultDto();
        try {
            this.userService.assignRole(rids,uid);
            resultDto.setCode(200);
            resultDto.setMsg("角色分配成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultDto.setCode(-1);
            resultDto.setMsg("角色分配失败");
        }
        return resultDto;
    }
}